﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dao
{
    public class XSHTGL_Dao
    {
        /// <summary>
        /// 新增一条记录
        /// </summary>
        /// <param name="entity">记录实体</param>
        /// <returns>操作影响的行数</returns>
        public int Add(XSHT entity)
        {
            using (var context = new Dao.ErpEntities())
            {
                context.XSHT.AddObject(entity);
                int a= context.SaveChanges();
                return a;
            }
        }

        /// <summary>
        /// 更新一条记录
        /// </summary>
        /// <param name="entity">记录实体</param>
        /// <returns>操作影响的行数</returns>
        public int Update(XSHT entity)
        {
            using (var context = new Dao.ErpEntities())
            {
                XSHT entity2 = context.XSHT.FirstOrDefault(c => c.ID == entity.ID);
                if (entity2 != null)
                {
                    //entity2 = entity;
                    context.XSHT.ApplyCurrentValues(entity);
                    return context.SaveChanges();
                }
                return 0;
            }
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="id">记录Id</param>
        /// <returns>操作影响的行数</returns>
        public int Delete(string id)
        {
            using (var context = new Dao.ErpEntities())
            {
                Guid guid = Guid.Parse(id);
                XSHT entity = context.XSHT.FirstOrDefault(c => c.ID == guid);
                if (entity != null)
                {
                    context.DeleteObject(entity);
                    return context.SaveChanges();
                }
                return 0;
            }
        }

        /// <summary>
        /// 查询一条记录
        /// </summary>
        /// <param name="id">记录Id</param>
        /// <returns>查询结果实体</returns>
        public XSHT Query(string id)
        {
            using (var context = new Dao.ErpEntities())
            {
                Guid guid = Guid.Parse(id);
                XSHT entity = context.XSHT.FirstOrDefault(c => c.ID == guid);
                return entity;
            }
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <returns>查询结果集</returns>
        public List<XSHT> QueryAll()
        {
            using (var context = new Dao.ErpEntities())
            {
                var q = from c in context.XSHT
                        select c;
                List<XSHT> list = new List<XSHT>();
                if (q.Count() > 0)
                {
                    foreach (var c in q)
                    {
                        list.Add(c);
                    }
                }
                return list;
            }
        }

        /// <summary>
        /// TODO 用存储过程提高效率
        /// 查询所有记录
        /// </summary>
        /// <returns>查询结果集</returns>
        public List<XSHT> QueryBySql(string bh, string khmc, string khlxr, string xsbmid, string xsyid, string dhsj, string chsj)
        {
            List<XSHT> list = new List<XSHT>();
            using (var context = new Dao.ErpEntities())
            {
                //var q = from j in context.XSHT
                //        select new
                //        {
                //            ID = j.ID,
                //            BH = j.BH,
                //            KHMC = j.KHMC,
                //            KHLXR = j.KHLXR,
                //            XSBMID = j.XSBMID,
                //            XSRYID = j.XSRYID,
                //            DHSJ = j.DHSJ,
                //            CHSJ = j.CHSJ,
                //            CHFS = j.CHFS,
                //            HTZT = j.HTZT
                //        };
                var q = from j in context.XSHT
                        select j;                       
                int a = q.Count();
                if (!string.IsNullOrEmpty(bh) && (!"0".Equals(bh)))
                {
                    Guid cuid = Guid.Parse(bh);
                    q = q.Where(c => c.ID == cuid);
                }

                if (!string.IsNullOrEmpty(khmc) && (!"0".Equals(khmc)))
                {
                    Guid cuid = Guid.Parse(khmc);
                    q = q.Where(c => c.ID == cuid);
                }
                if (!string.IsNullOrEmpty(khlxr) && (!"0".Equals(khlxr)))
                {
                    Guid cuid = Guid.Parse(khlxr);
                    q = q.Where(c => c.ID == cuid);
                }
                if (!string.IsNullOrEmpty(xsbmid) && (!"0".Equals(xsbmid)))
                {
                    Guid cuid = Guid.Parse(xsbmid);
                    q = q.Where(c => c.ID == cuid);
                }
                if (!string.IsNullOrEmpty(xsyid) && (!"0".Equals(xsyid)))
                {
                    Guid cuid = Guid.Parse(xsyid);
                    q = q.Where(c => c.ID == cuid);
                }

                if ((!string.IsNullOrEmpty(dhsj)) && (!"0".Equals(dhsj)))
                {
                    DateTime start = DateTime.Parse(dhsj);
                    q = q.Where(c => (c.DHSJ == start));
                }

                if ((!string.IsNullOrEmpty(chsj)) && (!"0".Equals(chsj)))
                {
                    DateTime end = DateTime.Parse(chsj);
                    q = q.Where(c => (c.CHSJ == end));
                }
                int b = q.Count();
                foreach (XSHT c in q)
                {
                    list.Add(c);
                }

                return list;
            }
        }

        /// <summary>
        /// TODO 用存储过程提高效率
        /// 查询所有记录
        /// </summary>
        /// <returns>查询结果集</returns>
        public List<object> QueryBySql2(string bh, string khmc, string khlxr, string xsbmid, string xsyid, string dhsj, string chsj)
        {
            List<object> list = new List<object>();
            using (var context = new Dao.ErpEntities())
            {
                var q = from j in context.XSHT
                        join d in context.DepartmentSet on j.XSBMID equals d.Id
                        join u in context.UserSet on j.XSRYID equals u.Id            
                        select new
                        {
                            ID = j.ID,
                            BH = j.BH,
                            KHMC = j.KHMC,
                            KHLXR = j.KHLXR,
                            XSBMID = j.XSBMID,
                            XSRYID = j.XSRYID,
                            XSBMMC=d.Name,  
                            XSBMMCID=d.Id,
                            XSRYMC=u.Name, 
                            XSRYMCID=u.Id,
                            DHSJ = j.DHSJ,
                            CHSJ = j.CHSJ,
                            CHFS = j.CHFS,
                            HTZT = j.HTZT
                        };     

                if (!string.IsNullOrEmpty(bh) && (!"0".Equals(bh)))
                {                    
                    q = q.Where(c => c.BH == bh);
                }

                if (!string.IsNullOrEmpty(khmc) && (!"0".Equals(khmc)))
                {
                    //Guid cuid = Guid.Parse(khmc);
                    q = q.Where(c => c.KHMC == khmc);
                }
                if (!string.IsNullOrEmpty(khlxr) && (!"0".Equals(khlxr)))
                {
                    Guid cuid = Guid.Parse(khlxr);
                    q = q.Where(c => c.ID == cuid);
                }
                if (!string.IsNullOrEmpty(xsbmid) && (!"0".Equals(xsbmid)))
                {
                    Guid cuid = Guid.Parse(xsbmid);
                    q = q.Where(c => c.XSBMMCID == cuid);
                }
                if (!string.IsNullOrEmpty(xsyid) && (!"0".Equals(xsyid)))
                {
                    Guid cuid = Guid.Parse(xsyid);
                    q = q.Where(c => c.XSRYMCID == cuid);
                }

                if ((!string.IsNullOrEmpty(dhsj)) && (!"0".Equals(dhsj)))
                {
                    DateTime start = DateTime.Parse(dhsj);
                    q = q.Where(c => (c.DHSJ == start));
                }

                if ((!string.IsNullOrEmpty(chsj)) && (!"0".Equals(chsj)))
                {
                    DateTime end = DateTime.Parse(chsj);
                    q = q.Where(c => (c.CHSJ == end));
                }
            
                foreach (object c in q)
                {
                    list.Add(c);
                }

                return list;
            }
        }

        public bool KTState(XSHT entity)
        {
            bool res = false;
            using (var context = new Dao.ErpEntities())
            {
                XSHT entity2 = context.XSHT.FirstOrDefault(c => c.ID == entity.ID);
                if (entity2 != null)
                {
                    //entity2 = entity;
                    context.XSHT.ApplyCurrentValues(entity);
                    context.SaveChanges();
                    res = true;
                }                
            }
            return res;
        }
    }
}
